# by lyzh 2015

if(__SYLIXOS_CMAKE_INCLUDED)
  return()
endif()
set(__SYLIXOS_CMAKE_INCLUDED TRUE)



set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SIZEOF_VOID_P 4)
set(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")            # -pic
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared")       # -shared
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-shared")         # +s, flag for exe link to use shared lib
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")       # -rpath
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")   # : or empty

set(CMAKE_LINK_LIBRARY_SUFFIX "")
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
set(CMAKE_SHARED_LIBRARY_PREFIX "lib")          # lib
set(CMAKE_SHARED_LIBRARY_SUFFIX ".a")           # .a
set(CMAKE_EXECUTABLE_SUFFIX ".elf")             # same suffix as if built using UseEcos.cmake
set(CMAKE_DL_LIBS "dl" )

set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")

set(CMAKE_DL_LIBS "dl")
set(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared")
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")
set(CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG "-Wl,-rpath-link,")
set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,")
set(CMAKE_EXE_EXPORTS_C_FLAG "-Wl,--export-dynamic")
set(CMAKE_PLATFORM_USES_PATH_WHEN_NO_SONAME 1)

# sylixOS support shared libs
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)

#
set(CMAKE_SHARED_LIBRARY_SUFFIX_C ".so")
set(CMAKE_SHARED_LIBRARY_SUFFIX_CXX ".so")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${SYLIXOS_SOURCE_DIR} )

include(Platform/UnixPaths)

get_property(_IN_TC GLOBAL PROPERTY IN_TRY_COMPILE)
if(CMAKE_C_COMPILER AND NOT  CMAKE_C_COMPILER_ID MATCHES "GNU" AND NOT _IN_TC)
  message(FATAL_ERROR "GNU gcc is required for SylixOS")
endif()
if(CMAKE_CXX_COMPILER AND NOT  "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" AND NOT _IN_TC)
  message(FATAL_ERROR "GNU g++ is required for SylixOS")
endif()


set(CMAKE_C_FLAGS_DEBUG -g -O0 )
set(CMAKE_C_FLAGS_RELEASE -O2 )

set(CMAKE_CXX_FLAGS_DEBUG -g -O0 )
set(CMAKE_CXX_FLAGS_RELEASE -O2 )

get_filename_component(SYLIXOS_LIBTARGET_DIRECTORY "${SYLIXOS_SYSTEM_TARGET_LIBRARY}" PATH)
include_directories(
  ${SYLIXOS_SOURCE_DIR}/SylixOS
  ${SYLIXOS_SOURCE_DIR}/SylixOS/include
  ${SYLIXOS_SOURCE_DIR}/SylixOS/include/inet
  ${SYLIXOS_SOURCE_DIR}/SylixOS/include/inet/ipv4
  ${SYLIXOS_SOURCE_DIR}/SylixOS/include/inet/ipv6
  )

set(CMAKE_C_FLAGS "-Wall -fmessage-length=0 -fsigned-char -fno-short-enums" )
set(CMAKE_CXX_FLAGS "-Wall -fno-exceptions -fno-rtti" )

set(CMAKE_SHARED_LINKER_FLAGS "-nostdlib -nostartfiles" )

add_definitions(-DSYLIXOS=1 -D__SYLIXOS_LITE -D__unix__)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-nostdlib -nostartfiles -fPIC -shared"  )

set(SYLIXOS_LIBTARGET_DIRECTORY ${SYLIXOS_BUILD_DIR})

# special link commands for SylixOS executables (application)
set(CMAKE_CXX_LINK_EXECUTABLE  "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> -fPIC -shared -nostdlib -nostartfiles -L${SYLIXOS_LIBTARGET_DIRECTORY} -lvpmpdm <LINK_LIBRARIES> -lm -lgcc ")
set(CMAKE_C_LINK_EXECUTABLE    "<CMAKE_C_COMPILER>   <FLAGS> <CMAKE_C_LINK_FLAGS>   <LINK_FLAGS> <OBJECTS> -o <TARGET> -fPIC -shared -nostdlib -nostartfiles -L${SYLIXOS_LIBTARGET_DIRECTORY} -lvpmpdm <LINK_LIBRARIES> -lm -lgcc ")
set(CMAKE_C_ARCHIVE_CREATE     "${CMAKE_AR} -r <TARGET> <OBJECTS>")
set(CMAKE_C_ARCHIVE_APPEND     "${CMAKE_AR} -r <TARGET> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH     "${CMAKE_AR} -r <TARGET> <OBJECTS>")

set(CMAKE_CXX_ARCHIVE_CREATE   "${CMAKE_AR} -r <TARGET> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_APPEND   "${CMAKE_AR} -r <TARGET> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_FINISH   "${CMAKE_AR} -r <TARGET> <OBJECTS>")

set(CMAKE_C_CREATE_SHARED_MODULE   "<CMAKE_C_COMPILER>    -nostdlib -r -o <TARGET> <OBJECTS> -lm -lgcc ")
set(CMAKE_CXX_CREATE_SHARED_MODULE "<CMAKE_CXX_COMPILER>  -nostdlib -r -o <TARGET> <OBJECTS> -lm -lgcc ")
# SylixOS support shared libs
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
set_property(GLOBAL PROPERTY LINKER_LANGUAGE CXX)

# to creating SylixOS keynel module.
function(add_sylixos_kernel_module modulename ... )
  set(cmd ${modulename} )
  set( src ${ARGV} )
  list(REMOVE_AT src 0 )
  add_library(${modulename} MODULE ${src} )
  set_target_properties( ${modulename}
    PROPERTIES
    C_LINK_FLAGS "-r"
    PREFIX ""
    SUFFIX ".ko"
    COMPILE_FLAGS "-fPIC"
    )
  # need to disable ranlib?
endfunction()


